package cn.edu.sdu.java.server.controllers;

import cn.edu.sdu.java.server.models.Course;
import cn.edu.sdu.java.server.models.CourseChoose;
import cn.edu.sdu.java.server.models.Student;
import cn.edu.sdu.java.server.payload.request.DataRequest;
import cn.edu.sdu.java.server.payload.response.DataResponse;
import cn.edu.sdu.java.server.payload.response.OptionItemList;
import cn.edu.sdu.java.server.repositorys.CourseChooseRepository;
import cn.edu.sdu.java.server.repositorys.CourseRepository;
import cn.edu.sdu.java.server.repositorys.StudentRepository;
import cn.edu.sdu.java.server.services.CourseChooseService;
import cn.edu.sdu.java.server.util.CommonMethod;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/courseChoose")
public class CourseChooseController {
    @Autowired
    private StudentRepository studentRepository;
    @Autowired
    private CourseRepository courseRepository;
    @Autowired
    private CourseChooseRepository courseChooseRepository;
    private final CourseChooseService courseChooseService;

    public CourseChooseController(CourseChooseService courseChooseService) {
        this.courseChooseService = courseChooseService;
    }

    @PostMapping("/getStudentItemOptionList")
    public OptionItemList getStudentItemOptionList(@Valid @RequestBody DataRequest dataRequest) {
        return courseChooseService.getStudentItemOptionList(dataRequest);
    }

    @PostMapping("/getCourseItemOptionList")
    public OptionItemList getCourseItemOptionList(@Valid @RequestBody DataRequest dataRequest) {
        return courseChooseService.getCourseItemOptionList(dataRequest);
    }
    @PostMapping("/getCourseChoose")
    public DataResponse getCourseChoose(@Valid @RequestBody DataRequest dataRequest) {
        return courseChooseService.getCourseChoose(dataRequest);
    }
    @PostMapping("/courseChooseSave")
    public DataResponse courseChooseSave(@Valid @RequestBody DataRequest dataRequest) {
        return courseChooseService.courseChooseSave(dataRequest);
    }
    @PostMapping("/courseChooseDelete")
    public DataResponse courseChooseDelete(@Valid @RequestBody DataRequest dataRequest) {
        return courseChooseService.courseChooseDelete(dataRequest);
    }
    @PostMapping("/courseSelectForStudent")
    public DataResponse courseSelectForStudent(@Valid@RequestBody DataRequest dataRequest){
        String courseNum= dataRequest.getString("courseNum");
        String courseName= dataRequest.getString("courseName");
        String studentNum= dataRequest.getString("studentNum");
        String studentName= dataRequest.getString("studentName");
        Course c;
        Student s;
        Optional<Course> op1=courseRepository.findByNumAndName(courseNum,courseName);
        if(!op1.isPresent()){
            return CommonMethod.getReturnMessageError("找不到该课程");
        }
        Optional<Student> op2=studentRepository.fByNumAndName(studentNum,studentName);
        if(!op2.isPresent()){
            return CommonMethod.getReturnMessageError("没有该学生");
        }
        Optional<CourseChoose> sOp=courseChooseRepository.totalSearch(courseNum,studentNum);
        if(sOp.isPresent()){
            return CommonMethod.getReturnMessageError("已经选了这门课");
        }
        c=op1.get();
        s=op2.get();
        CourseChoose courseChoose=new CourseChoose();
        courseChoose.setStudent(s);
        courseChoose.setCourse(c);
        courseChooseRepository.save(courseChoose);
        return CommonMethod.getReturnMessageOK();
    }
    @PostMapping("/courseDeleteForStudent")
    public DataResponse DeleteCourseChoose(@Valid @RequestBody DataRequest dataRequest) {
        String courseNum= dataRequest.getString("courseNum");
        String courseName= dataRequest.getString("courseName");
        String num= dataRequest.getString("studentNum");
        String name= dataRequest.getString("studentName");
        if(courseNum==null){
            courseNum="";
        }
        if(courseName==null){
            courseName="";
        }
        if(num==null){
            num="";
        }
        if(name==null){
            name="";
        }
        Optional<Course> op1=courseRepository.findByNumAndName(courseNum,courseName);
        Optional<Student> op2=studentRepository.fByNumAndName(num,name);
        if(!op1.isPresent() || !op2.isPresent()){
            return CommonMethod.getReturnMessageError("学生或课程不存在");
        }
        Optional<CourseChoose> op=courseChooseRepository.findOpByStudentCourse(op2.get().getStudentId(),op1.get().getCourseId());
        if(!op.isPresent()){
            return CommonMethod.getReturnMessageError("没有这条选课记录");
        }
        Optional<CourseChoose> sOp=courseChooseRepository.totalSearch(courseNum,num);
        if(!sOp.isPresent()){
            return CommonMethod.getReturnMessageError("没有选这门课哦");
        }
        CourseChoose courseChoose=op.get();
        courseChooseRepository.delete(courseChoose);

        return CommonMethod.getReturnMessageOK();
    }
    @PostMapping("/getCourseSelectedList")
    public DataResponse getCourseSelectedList(@Valid@RequestBody DataRequest dataRequest){
        String num= dataRequest.getString("studentNum");
        String name= dataRequest.getString("name");
        List<CourseChoose> selectedList=courseChooseRepository.getByStudent(num,name);
        List dataList = new ArrayList();
        Map m;
        Course c;
        for (CourseChoose ch:selectedList) {
            m = new HashMap();
            c=ch.getCourse();
            m.put("courseId", c.getCourseId()+"");
            m.put("num",c.getNum());
            m.put("name",c.getName());
            m.put("credit",c.getCredit());
            m.put("time",c.getCoursePath());
            m.put("position",ch.getPosition());
            m.put("preCourse",c.getPreCourse());

            dataList.add(m);
        }
        return CommonMethod.getReturnData(dataList);
    }
    @PostMapping("/getNoSCourseForStudentList")
    public DataResponse courseNotSelectedForStudent(@Valid@RequestBody DataRequest dataRequest){
        String num= dataRequest.getString("studentNum");
        String name= dataRequest.getString("name");
        List<Course> all=courseRepository.findAll();
        List<CourseChoose> selectedList=courseChooseRepository.getByStudent(num,name);
        List<Course> selectedCourses=new ArrayList<>();
        for(CourseChoose ch:selectedList){
            if(!selectedCourses.contains(ch.getCourse())){
                selectedCourses.add(ch.getCourse());
            }
        }
        List dataList = new ArrayList();
        Map m;
        Course c;
        for (Course course:all) {
            if(!selectedCourses.contains(course)){
                m = new HashMap();
                c=course;
                m.put("courseId", c.getCourseId()+"");
                m.put("num",c.getNum());
                m.put("name",c.getName());
                m.put("credit",c.getCredit());
                m.put("time",c.getCoursePath());
                m.put("preCourse",c.getPreCourse());
                dataList.add(m);
            }



        }
        return CommonMethod.getReturnData(dataList);
    }
}
